exports = this
exports.CPANEL = exports.CPANEL || {}

# 显示提示消息
exports.CPANEL.alert = (type, message, opts) ->
  options = {extendedTimeOut: 0, timeOut: 0, positionClass: 'toast-top-right', debug: false}
  $.extend(options, opts)

  switch type
    when 'success'
      options.timeOut = 2000
      toastr.success(message, null, options)
    when 'error'
      toastr.error(message, null, options)

# 显示提示消息
exports.CPANEL.alert_success = (message, opts) ->
  options = {extendedTimeOut: 0, timeOut: 0, positionClass: 'toast-top-right', debug: false}
  $.extend(options, opts)
  options.timeOut = 2000
  toastr.success(message, null, options)

# 显示提示消息
exports.CPANEL.alert_error = (message, opts) ->
  options = {extendedTimeOut: 0, timeOut: 0, positionClass: 'toast-top-right', debug: false}
  $.extend(options, opts)
  options.timeOut = 2000
  toastr.error(message, null, options)

# dataTable插件翻译
exports.CPANEL.datatableLanguage =
  sProcessing: "处理中..."
  sLengthMenu: "显示 _MENU_ 项结果"
  sZeroRecords: "没有匹配结果"
  sInfo: "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项"
  sInfoEmpty: "显示第 0 至 0 项结果，共 0 项"
  sInfoFiltered: "(由 _MAX_ 项结果过滤)"
  sInfoPostFix: ""
  sSearch: "搜索:"
  sUrl: ""
  sEmptyTable: "表中数据为空"
  sLoadingRecords: "载入中..."
  sInfoThousands: ","
  oPaginate:
    sFirst: "首页"
    sPrevious: "上页"
    sNext: "下页"
    sLast: "末页"
  oAria:
    sSortAscending: ": 以升序排列此列"
    sSortDescending: ": 以降序排列此列"

# 七牛云上传
class exports.CPANEL.QiniuUploader
  constructor: (@options) ->
    @_init(@options)

  _init: (options)->
    BeforeUpload_callback = options.BeforeUpload_callback
    UploadProgress_callback = options.UploadProgress_callback
    UploadComplete_callback = options.UploadComplete_callback
    FileUploaded_callback = options.FileUploaded_callback
    Error_callback = options.Error_callback
    Key_callback = options.Key_callback
    container = options.container
    drop_element = options.drop_element
    browse_button = options.browse_button
    Qiniu.uploader(
      runtimes: 'html5,flash,html4'
      browse_button: browse_button
      container: container
      drop_element: drop_element
      max_file_size: '1000mb'
      flash_swf_url: 'http://soss.zhubaijia.com/libs/plupload/2.1.8/Moxie.cdn.swf'
      dragdrop: true
      chunk_size: '4mb'
      multi_selection: mOxie.Env.OS.toLowerCase() != "ios"
      uptoken_url: "/qiniu/uptoken"
      domain: "<%= ::AnAnCommon::QINIU_BUCKET_DOMAIN %>/"
      get_new_uptoken: true
      auto_start: true
      log_level: 5
      init:
        FilesAdded: (up, files) ->
          FilesAdded_callback(up,file) if typeof(FilesAdded_callback) == "function"
        BeforeUpload: (up, file) ->
          BeforeUpload_callback(up,file) if typeof(BeforeUpload_callback) == "function"
        UploadProgress: (up, file) ->
          UploadProgress_callback(up,file) if typeof(UploadProgress_callback) == "function"
        UploadComplete: ->
          CPANEL.alert_success("上传成功！")
          UploadComplete_callback() if typeof(UploadComplete_callback) == "function"
        FileUploaded: (up, file, info) ->
          FileUploaded_callback(up, file, info) if typeof(FileUploaded_callback) == "function"
        Error: (up, err, errTip) ->
          CPANEL.alert_error("上传失败！")
          Error_callback(up, err, errTip) if typeof(Error_callback) == "function"
        Key: (up, file) ->
          if typeof(Key_callback) == "function"
            Key_callback(up,file)
    )

# 商品选择框
class exports.CPANEL.ProductsModal
  constructor: (@options) ->
    @_init(@options)

  _init: (options)->
    modal_id = options.modal_id
    modal_confirm_callback = options.modal_confirm_callback

    # 模态框初始化
    $("#product_select_table_modal").modal(show: false)

    # 打开按钮监听
    $("#" + modal_id).click ->
      # $("#product_select_table").css('width', '100%')
      $("#product_select_table_modal").modal('show')

    # datatable 初始化
    $("#product_select_table").DataTable
      pageLength: 10
      bProcessing: true
      bServerSide: true
      stateSave: true
      select: true
      responsive: true
      sAjaxSource: $("#product_select_table").data('source')
      language: CPANEL.datatableLanguage
      order: [[3, 'desc']]
      columns: [
        { data: "image_url" }
        { data: "name" }
        { data: "price" }
        { data: "created_at" }
      ]
      columnDefs: [
        { orderable: false, targets: 0 }
        { orderable: false, targets: 1 }
        { orderable: false, targets: 2 }
      ]

    # datatable 选中初始化
    $("#product_select_table tbody").on 'click', 'tr', ->
      if ($(this).hasClass('selected'))
        $(this).removeClass('selected')
      else
        $("#product_select_table").DataTable().$('tr.selected').removeClass('selected')
        $(this).addClass('selected')

    # 模态框确定按钮回调
    $("#product_select_table_confirm_bn").click ->
      data = $("#product_select_table").DataTable().row('.selected').data()
      if (typeof(modal_confirm_callback) == "function" && typeof(data)=="object")
        modal_confirm_callback(data)
        $("#product_select_table_modal").modal('hide')
      else
        CPANEL.alert_success("请选择商品信息！")

# 终端选择框
class exports.CPANEL.MachinesModal
  constructor: (@options) ->
    @_init(@options)

  _init: (options)->
    modal_id = options.modal_id
    modal_confirm_callback = options.modal_confirm_callback

    # 模态框初始化
    $("#machines_select_table_modal").modal(show: false)

    # 打开按钮监听
    $("#" + modal_id).click ->
      $("#machines_select_table_modal").modal('show')

    # datatable 初始化
    $("#machines_select_table").DataTable
      pageLength: 10
      bProcessing: true
      bServerSide: true
      stateSave: true
      select: true
      responsive: true
      sAjaxSource: $("#machines_select_table").data('source')
      language: CPANEL.datatableLanguage
      order: [[3, 'desc']]
      columns: [
        { data: "code" }
        { data: "name" }
        { data: "cell_num" }
        { data: "created_at" }
      ]
      columnDefs: [
        { orderable: false, targets: 0 }
        { orderable: false, targets: 1 }
        { orderable: false, targets: 2 }
      ]

    # datatable 选中初始化
    $("#machines_select_table tbody").on 'click', 'tr', ->
      if ($(this).hasClass('selected'))
        $(this).removeClass('selected')
      else
        $("#machines_select_table").DataTable().$('tr.selected').removeClass('selected')
        $(this).addClass('selected')

    # 模态框确定按钮回调
    $("#machines_select_table_confirm_bn").click ->
      data = $("#machines_select_table").DataTable().row('.selected').data()
      if (typeof(modal_confirm_callback) == "function" && typeof(data)=="object")
        modal_confirm_callback(data)
        $("#machines_select_table_modal").modal('hide')
      else
        CPANEL.alert_success("请选择终端信息！")


# 格子选择框
class exports.CPANEL.GridsModal
  constructor: (@options) ->
    @_init(@options)

  _init: (options)->
    modal_id = options.modal_id
    modal_confirm_callback = options.modal_confirm_callback

    # 模态框初始化
    $("#grids_select_table_modal").modal(show: false)

    # 打开按钮监听
    $("#" + modal_id).click ->
      $("#grids_select_table_modal").modal('show')

    # datatable 初始化
    $("#grids_select_table").DataTable
      pageLength: 10
      bProcessing: true
      bServerSide: true
      stateSave: true
      select: true
      responsive: true
      sAjaxSource: $("#grids_select_table").data('source')
      language: CPANEL.datatableLanguage
      order: [[2, 'desc']]
      columns: [
        { data: "code" }
        { data: "name" }
        { data: "created_at" }
      ]
      columnDefs: [
        { orderable: false, targets: 0 }
        { orderable: false, targets: 1 }
        { orderable: false, targets: 2 }
      ]

    # datatable 选中初始化
    $("#grids_select_table tbody").on 'click', 'tr', ->
      if ($(this).hasClass('selected'))
        $(this).removeClass('selected')
      else
        $("#grids_select_table").DataTable().$('tr.selected').removeClass('selected')
        $(this).addClass('selected')

    # 模态框确定按钮回调
    $("#grids_select_table_confirm_bn").click ->
      data = $("#grids_select_table").DataTable().row('.selected').data()
      if (typeof(modal_confirm_callback) == "function" && typeof(data)=="object")
        modal_confirm_callback(data)
        $("#grids_select_table_modal").modal('hide')
      else
        CPANEL.alert_success("请选择格子信息！")
